﻿using System;
using TG.Timer;
using UnityEngine;

namespace TG
{
    public class LambPlayer : EntityBase
    {
        public override MapItemType mapItemType => MapItemType.Lamb;

        private float x;
        private float y;

        #region 移动旋转参数

        /// <summary>
        /// 移动速度
        /// </summary>
        public float moveSpeed;

        /// <summary>
        /// 旋转速度
        /// </summary>
        public float vAngleSpeed;

        #endregion

        #region 特殊道具

        private bool haveVacuumCleaner;
        // private int vacuumCleanerCD;

        private bool haveCleat;
        // private int cleatCD;

        #endregion

        #region GET/SET

        public bool GetVacuumCleaner()
        {
            return haveVacuumCleaner;
        }

        public void SetVacuumCleaner(bool value)
        {
            haveVacuumCleaner = value;
            // if (haveVacuumCleaner)
            //     vacuumCleanerCD = Default.VacuumCleanerTime;
        }

        public bool GetCleat()
        {
            return haveCleat;
        }

        public void SetCleat(bool value)
        {
            haveCleat = value;
            // if (haveCleat)
            //     cleatCD = Default.CleatTime;
        }

        #endregion

        public override void Init(MapCell mapCell)
        {
            transform.position = Vector3.zero;
            // TimerManager.Instance.AddSecondsElapsed(SecondsElapsed);
        }

        // void SecondsElapsed()
        // {
        //     if (haveVacuumCleaner)
        //     {
        //         vacuumCleanerCD--;
        //         if (vacuumCleanerCD <= 0)
        //             haveVacuumCleaner = false;
        //     }
        //
        //     if (haveCleat)
        //     {
        //         cleatCD--;
        //         if (cleatCD <= 0)
        //             haveCleat = false;
        //     }
        // }

        public void Update()
        {
            x = BalanceInputManager.Instance.hAxis;
            y = BalanceInputManager.Instance.vAxis;

            HandTurnModel();
            Move();
            CheckVacuumCleaner();
        }

        /// <summary>
        /// 旋转
        /// </summary>
        private void HandTurnModel()
        {
            if (BalanceInputManager.Instance.inputUp == 0)
                y = -y;

            if (x == 0 && y == 0) return;

            var eulerAngle = new Vector3(0, transform.eulerAngles.y + x * vAngleSpeed, 0);

            var targetRot = Quaternion.Euler(eulerAngle);

            transform.rotation = Quaternion.Slerp(transform.rotation, targetRot, Time.deltaTime);
        }

        private void Move()
        {
            var addPos = transform.forward * (moveSpeed * Time.deltaTime);
            transform.position += addPos;
        }

        public void Reset()
        {
            transform.position = Vector3.zero;
        }

        private void CheckVacuumCleaner()
        {
            if (!haveVacuumCleaner) return;

            var allEntity = GameManager.Instance.GetGameData().allEntity;

            for (int i = allEntity.Count - 1; i >= 0; i--)
            {
                var entityBase = allEntity[i];
                if (entityBase is VegetableEntity vegetableEntity)
                {
                    var dis = Vector3.Distance(transform.position, vegetableEntity.transform.position);
                    if (dis <= Default.VacuumCleanerRadius + 1)
                        vegetableEntity.MoveToPlayer(dis);
                }
            }
        }
    }
}